package com.yuren.codecrushlearn.Q5300;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with Intellij IDEA.
 * Description:
 * <p>  电话号码的字母组合
 * 给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
 * <p>
 * 给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。
 * <p>
 * <p>
 * @author 10204
 * @date 2025-05-31 23:19
 */
public class Q5393 {
    private static final Map<Character, String> digitToChars = new HashMap<>();

    static {
        digitToChars.put('1', "");
        digitToChars.put('2', "abc");
        digitToChars.put('3', "def");
        digitToChars.put('4', "ghi");
        digitToChars.put('5', "jkl");
        digitToChars.put('6', "mno");
        digitToChars.put('7', "pqrs");
        digitToChars.put('8', "tuv");
        digitToChars.put('9', "wxyz");
    }

    public List<String> digitsToChars(String digits) {
        List<String> res = new ArrayList<>();

        if (digits == null || digits.length() == 0) {
            return res;
        }
        dfs(digits, 0, new char[digits.length()], res);
        return res;
    }

    private void dfs(String digits, int index, char[] strtmp, List<String> res) {
        if (index == digits.length()) {
            res.add(String.valueOf(strtmp));
            return;
        }

        String chars = digitToChars.get(digits.charAt(index));
        for (char ch : chars.toCharArray()) {
            strtmp[index] = ch;
            dfs(digits, index + 1, strtmp, res);
        }
    }
}
